package com.edu.policy.mapper;

import com.edu.policy.model.UmsAdminRoleRelation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.edu.policy.model.UmsMenu;
import com.edu.policy.model.UmsResource;
import com.edu.policy.model.UmsRole;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* @author chenhaizhao
* @description 针对表【ums_admin_role_relation(后台用户和角色关系表)】的数据库操作Mapper
* @createDate 2024-09-24 16:13:52
* @Entity com.edu.policy.domain.UmsAdminRoleRelation
*/
public interface UmsAdminRoleRelationMapper extends BaseMapper<UmsAdminRoleRelation> {

    /**
     * 获取资源相关用户ID列表
     */
    @Select("SELECT ar.admin_id FROM ums_role_resource_relation rr " +
            "LEFT JOIN" +
            " ums_admin_role_relation ar ON rr.role_id = ar.role_id " +
            "WHERE rr.resource_id = #{resourceId}")
    List<Long> getAdminIdList(@Param("resourceId") Long resourceId);
    /**
     * 根据后台用户ID获取菜单
     */
    @Select("SELECT" +
            "            m.id," +
            "            m.parent_id parentId," +
            "            m.create_time createTime," +
            "            m.title," +
            "            m.level," +
            "            m.sort," +
            "            m.name," +
            "            m.icon," +
            "            m.hidden" +
            "        FROM" +
            "            ums_admin_role_relation arr" +
            "                LEFT JOIN ums_role r ON arr.role_id = r.id" +
            "                LEFT JOIN ums_role_menu_relation rmr ON r.id = rmr.role_id" +
            "                LEFT JOIN ums_menu m ON rmr.menu_id = m.id" +
            "        WHERE" +
            "            arr.admin_id = #{adminId}" +
            "          AND m.id IS NOT NULL" +
            "        GROUP BY" +
            "            m.id")
    List<UmsMenu> getMenuList(@Param("adminId") Long adminId);


    /**
     * 根据角色ID获取菜单
     */
    @Select("SELECT" +
            "            m.id ," +
            "            m.parent_id parentId ," +
            "            m.create_time createTime ," +
            "            m.title ," +
            "            m.level ," +
            "            m.sort ," +
            "            m.name ," +
            "            m.icon ," +
            "            m.hidden " +
            "        FROM" +
            "             ums_role_menu_relation rmr" +
            "                LEFT JOIN ums_menu m ON rmr.menu_id = m.id" +
            "        WHERE" +
            "            rmr.role_id = #{roleId}" +
            "          AND m.id IS NOT NULL" +
            "        GROUP BY" +
            "            m.id")
    List<UmsMenu> getMenuListByRoleId(@Param("roleId") Long roleId);

    /**
     * 根据角色ID获取资源
     */
    @Select("SELECT" +
            "            r.id ," +
            "            r.create_time createTime," +
            "            r.name ," +
            "            r.url ," +
            "            r.description ," +
            "            r.category_id " +
            "        FROM" +
            "            ums_role_resource_relation rrr" +
            "                LEFT JOIN ums_resource r ON rrr.resource_id = r.id" +
            "        WHERE" +
            "            rrr.role_id = #{roleId}" +
            "          AND r.id IS NOT NULL" +
            "        GROUP BY" +
            "            r.id")
    List<UmsResource> getResourceListByRoleId(@Param("roleId") Long roleId);
    /**
     * 获取用于所有角色
     */
    @Select("select r.*" +
            "        from ums_admin_role_relation ar left join ums_role r on ar.role_id = r.id" +
            "        where ar.admin_id = #{adminId}")
    List<UmsRole> getRoleList(@Param("adminId") Long adminId);

    /**
     * 获取用户所有可访问资源
     */
    @Select("SELECT" +
            "            ur.id ," +
            "            ur.create_time createTime," +
            "            ur.name ," +
            "            ur.url ," +
            "            ur.description ," +
            "            ur.category_id categoryId" +
            "        FROM" +
            "            ums_admin_role_relation ar" +
            "        LEFT JOIN ums_role r ON ar.role_id = r.id" +
            "        LEFT JOIN ums_role_resource_relation rrr ON r.id = rrr.role_id" +
            "        LEFT JOIN ums_resource ur ON ur.id = rrr.resource_id" +
            "        WHERE" +
            "            ar.admin_id = #{adminId}" +
            "        AND ur.id IS NOT NULL" +
            "        GROUP BY" +
            "            ur.id")
    List<UmsResource> getResourceList(@Param("adminId") Long adminId);
}




